package com.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.Special;
import com.beans.Staff;
import com.services.SpecialService;
import com.services.StaffService;

/**
 * Servlet implementation class SpecialServlet
 */
@WebServlet("/special")
public class SpecialServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private  SpecialService special= new SpecialService(); 
    private StaffService staff = new StaffService();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SpecialServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		String message = request.getParameter("message");
		if(message!=null) {
			request.setAttribute("message", message);
			List<Special> list = special.selectAll();
			request.setAttribute("list", list);
			request.getRequestDispatcher("special.jsp").forward(request, response);
		}
		else
		{
			
		
		if (method == null) {
			method = "selectAll";
		}
		switch (method) {
		case "selectAll":
			List<Special> list = special.selectAll();
			request.setAttribute("list", list);
			request.getRequestDispatcher("special.jsp").forward(request, response);
			break;
		case "findById":
			String staffSnotStr = request.getParameter("staffSno");
			int staffSno = Integer.parseInt(staffSnotStr);
			request.setAttribute("special", special.findById(staffSno));
			request.getRequestDispatcher("updateSpecial.jsp").forward(request, response);
			break;
		case "add":
			List<Staff> Stafflist = staff.selectAll();
			List<Staff> newlist=new ArrayList<>();
			list = special.selectAll();
			boolean flag=false;
			for(int i=0;i<Stafflist.size();i++) {
				flag=false;
				for(int j=0;j<list.size();j++)
				{
					if(Stafflist.get(i).getStaffSno()==list.get(j).getStaffSno())
					{
						flag=true;
						break;
					}
					
				}
				if(flag==false)
					newlist.add(Stafflist.get(i));
			}
			if(newlist.size()==0) {
				message="<script type='text/javascript' >alert('There are no employees to add');</script>";
				request.setAttribute("message",message);
				response.sendRedirect("/salary_manage/special?message="+message);;
			}
			else
			{
				request.setAttribute("list", newlist);
				request.getRequestDispatcher("addSpecial.jsp").forward(request, response);
			}
			
			
			break;

		}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String method = request.getParameter("method");
		
		String staffSnoStr=request.getParameter("staffSno");
		int staffSno=Integer.parseInt(staffSnoStr);
		
		String childNumStr=request.getParameter("childNum");
		int childNum=Integer.parseInt(childNumStr);
		
		String conEduStr=request.getParameter("conEdu");
		int conEdu=Integer.parseInt(conEduStr);
		
		String houseLoanStr=request.getParameter("houseLoan");
		int houseLoan=Integer.parseInt(houseLoanStr);
		
		String rentStr=request.getParameter("rent");
		int rent=Integer.parseInt(rentStr);
		
		String oldStr=request.getParameter("old");
		int old=Integer.parseInt(oldStr);
		switch(method) {
		case"add":
			special.add(staffSno, childNum, conEdu, houseLoan, rent, old);
			break;
		case"update":
			special.update(staffSno, childNum, conEdu, houseLoan, rent, old);
		}
		response.sendRedirect("/salary_manage/special");
	}

}
